package cn.xk.ymcc.mq;

import cn.xk.ymcc.domain.MediaFile;
import cn.xk.ymcc.service.impl.IMediaFileService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/*
    消息模式
        MessageModel
            CLUSTERING：(默认) 集群 -- 多个服务去监听同一个topic,消息只会被一个服务所消费
            BROADCASTING：广播     -- 多个消费者同时监听同一个topic,多个消费者都会消费一次
 */
@Component
@RocketMQMessageListener(
        consumerGroup = "service-consumer-media",
        topic = "media-topic",
        selectorExpression = "media-tags",//tags
        messageModel = MessageModel.CLUSTERING
)
public class MediaFileConsumer implements RocketMQListener<String> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(String message) {
        /**
         * 从MQ里获取信息
         * 1.把json字符串进行数据还原 MediaFile对象
         */
        MediaFile mediaFile = JSON.parseObject(message, MediaFile.class);
        //推送流方法
        mediaFileService.handleFile2m3u8(mediaFile);
    }


}
